﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using KTool2.CustomEvent;
using KTool2.Common;
using KTool2.Exceptions;
using KTool2.Impl;
using KTool2.Utility;

namespace KTool2
{
    //public delegate void UpdateTextOnInfoWindow(Core.IKanjiWord kWord, List<int> rateShowingList);
    /// <summary>
    /// Interaction logic for InfoWindow.xaml
    /// </summary>
    public partial class InfoWindow : Window
    {
        public delegate void ActivationHandler();
        public event ActivationHandler ParentActivated;
        public delegate void WindowShowHideHandler(object sender, WindowShowHideEventArgs e);
        public event WindowShowHideHandler ShowHideChanged;
        private System.Windows.Media.Animation.DoubleAnimation t_textAnimationBegin;
        private System.Windows.Media.Animation.DoubleAnimation t_textAnimationEnd;
        //private IKanjiWord kWord;
        private bool isShowing;
        private volatile static InfoWindow instance;

        private InfoWindow()
        {
            InitializeComponent();
            InitializeTextAnimation();
            this.grdStatus.Background = KTool2.Utility.ImageManager.StatusBackground;
            this.Top = System.Windows.SystemParameters.PrimaryScreenHeight;
            this.Show();
            this.isShowing = true;
            this.chkbxKeep.IsChecked = KTool2.Utility.AppSetting.Instance.KeepTranslation;
        }

        public static InfoWindow Instance
        {
            get
            {
                if (instance == null)
                {
                    System.Threading.Mutex mutex = new System.Threading.Mutex();
                    mutex.WaitOne();

                    if (instance == null)
                    {
                        instance = new InfoWindow();
                    }

                    mutex.Close();
                }

                return instance;
            }
        }

        public static bool IsNull
        {
            get { return instance == null; }
        }

        public void ZoomIn()
        {
            this.grdMain.Width = this.window.Width;
            this.grdMain.Height = this.window.Height;

            this.window.Height = this.window.Height * 1.5;
            this.window.Width = this.window.Width * 1.5;

            ScaleTransform st = new ScaleTransform();
            st.ScaleX = 1.5;
            st.ScaleY = 1.5;
            st.CenterX = this.grdMain.Width / 2;
            st.CenterY = this.grdMain.Height / 2;

            this.grdMain.RenderTransform = st;
        }

        public void ZoomOut()
        {
            this.window.Height = this.window.Height * 2.0 / 3;
            this.window.Width = this.window.Width * 2.0 / 3;

            ScaleTransform st = new ScaleTransform();
            st.ScaleX = 1;
            st.ScaleY = 1;
            st.CenterX = this.grdMain.Width / 2.0;
            st.CenterY = this.grdMain.Height / 2.0;

            this.grdMain.RenderTransform = st;
        }

        private void InitializeTextAnimation()
        {
            this.t_textAnimationBegin = new System.Windows.Media.Animation.DoubleAnimation();
            this.t_textAnimationBegin.To = 0;
            this.t_textAnimationBegin.Duration = TimeSpan.FromSeconds(Constant.TEXT_ANIMATION_TIME);
            //this.t_textAnimationBegin.Completed += new EventHandler(t_textAnimationBegin_Completed);

            this.t_textAnimationEnd = new System.Windows.Media.Animation.DoubleAnimation();
            this.t_textAnimationEnd.To = 1;
            this.t_textAnimationEnd.Duration = TimeSpan.FromSeconds(Constant.TEXT_ANIMATION_TIME);
        }

        public void SetTextEnd(KanjiWord kWord)
        {
            this.UpdateText(kWord);

            if (AppSetting.Instance.RateShowingArray[kWord.MemRate - 1] || this.chkbxKeep.IsChecked.Value)
            {
                if (!this.isShowing)
                {
                    this.Show();
                    this.isShowing = true;
                }

                this.txtEnglish.BeginAnimation(TextBlock.OpacityProperty, this.t_textAnimationEnd);
                this.txtHanNom.BeginAnimation(TextBlock.OpacityProperty, this.t_textAnimationEnd);
                this.txtHiragana.BeginAnimation(TextBlock.OpacityProperty, this.t_textAnimationEnd);
                this.txtViet.BeginAnimation(TextBlock.OpacityProperty, this.t_textAnimationEnd);

                this.ShowHideChanged(this, new WindowShowHideEventArgs(this.isShowing));
                return;
            }

            if (this.isShowing)
            {
                this.Hide();
                this.isShowing = false;
                this.ShowHideChanged(this, new WindowShowHideEventArgs(this.isShowing));
            }
        }

        public void HideWindow()
        {
            this.Hide();
            this.isShowing = false;
            this.ShowHideChanged(this, new WindowShowHideEventArgs(this.isShowing));
        }

        public void SetTextBegin()
        {
            //this.kWord = kWord;

            //foreach (int item in rateShowingList)
            //{
            //    if (item == kWord.MemRate) // need to show up details
            //    {
            //        if (!this.isShowing)
            //        {
            //            this.Show();
            //            this.isShowing = true;
            //            this.ShowHideChanged(this, new WindowShowHideEventArgs(this.isShowing));
            //        }

                    this.txtEnglish.BeginAnimation(TextBlock.OpacityProperty, this.t_textAnimationBegin);
                    this.txtHanNom.BeginAnimation(TextBlock.OpacityProperty, this.t_textAnimationBegin);
                    this.txtHiragana.BeginAnimation(TextBlock.OpacityProperty, this.t_textAnimationBegin);
                    this.txtViet.BeginAnimation(TextBlock.OpacityProperty, this.t_textAnimationBegin);
            //    }
            //    else // need to hide details
            //    {
            //        if (this.isShowing)
            //        {
            //            this.Hide();
            //            this.isShowing = false;
            //            this.ShowHideChanged(this, new WindowShowHideEventArgs(this.isShowing));
            //        }

            //        this.UpdateText();
            //    }
            //}
        }

        private void UpdateText(KanjiWord kWord)
        {
            try
            {
                KanjiWordProcessor.GetKanji(ref kWord);
                this.txtEnglish.Text = kWord.English;
                this.txtHanNom.Text = kWord.HanNom;
                this.txtHiragana.Text = kWord.Hiragana;
                this.txtViet.Text = kWord.Viet;
            }
            catch (DBException ex)
            {
                this.ShowErrorMessage("Error while getting kanji information:\n" + ex.Description);
            }
        }

        public void DeactivateWindow()
        {
            this.grdStatus.Background = KTool2.Utility.ImageManager.StatusBackgroundDeactivated;
        }

        public void ActivateWindow()
        {
            this.grdStatus.Background = KTool2.Utility.ImageManager.StatusBackground;
        }

        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);

            if (this.chkbxKeep.IsMouseOver)
            {
                this.chkbxKeep.IsChecked = !this.chkbxKeep.IsChecked;
            }

            try
            {
                this.ParentActivated();
            }
            catch (Exception ex)
            { 
                // this is when event this.Activated() hasn't been register yet.
            }
        }

        public void ShowWindow()
        {
            this.Show();
            this.isShowing = true;

            this.txtEnglish.BeginAnimation(TextBlock.OpacityProperty, this.t_textAnimationEnd);
            this.txtHanNom.BeginAnimation(TextBlock.OpacityProperty, this.t_textAnimationEnd);
            this.txtHiragana.BeginAnimation(TextBlock.OpacityProperty, this.t_textAnimationEnd);
            this.txtViet.BeginAnimation(TextBlock.OpacityProperty, this.t_textAnimationEnd);

            this.ShowHideChanged(this, new WindowShowHideEventArgs(this.isShowing));
        }

        private void ShowErrorMessage(string message)
        {
            MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }

        private void chkbxKeep_Checked(object sender, RoutedEventArgs e)
        {
            Utility.AppSetting.Instance.KeepTranslation = true;
        }

        private void chkbxKeep_Unchecked(object sender, RoutedEventArgs e)
        {
            Utility.AppSetting.Instance.KeepTranslation = false;
        }
    }
}
